home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / clsres1a / clsresiz.cls next >
Encoding:
Visual Basic class definition  |  1999-09-04  |  15.9 KB  |  309 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "clsResize"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. ' ======================================================================================== '
  11. ' Component : clsResize                                               Created : 01/05/1999 '
  12. ' File Name : clsResize.cls                                           Author  : A D Moss   '
  13. '                                                                                          '
  14. ' Purpose   : Resize and Reposition all controls on a Form.                                '
  15. ' ======================================================================================== '
  16. '                 Copyright ⌐ 1999 - Adam David Moss - All Rights Reserved                 '
  17. '                                                                                          '
  18. ' ======================================================================================== '
  19. '                                                                                          '
  20. Option Explicit
  21. ' ======================================================================================== '
  22. ' API Declarations.                                                                        '
  23. ' ======================================================================================== '
  24. Private Declare Function LockWindowUpdate Lib "user32.dll" (ByVal hwndLock As Long) As Long
  25.  
  26. ' ======================================================================================== '
  27. ' Constant Declarations.                                                                   '
  28. ' ======================================================================================== '
  29. Private Const SSTAB_DIALOG_OFFSET As Long = 75000
  30. Private Const SSTAB_TYPE_NAME As String = "SSTAB"
  31.  
  32. Private Const ResizeRepositionCommand As String = "@"
  33. Private Const ResizeRepositionLeft As String = "L"
  34. Private Const ResizeRepositionTop As String = "T"
  35. Private Const ResizeRepositionWidth As String = "W"
  36. Private Const ResizeRepositionHeight As String = "H"
  37.  
  38. ' ======================================================================================== '
  39. ' Type Declarations.                                                                       '
  40. ' ======================================================================================== '
  41. Private Type ControlPosition
  42.   ControlInstance As Control                         'Reference to the control instance.
  43.   OriginalLeft As Long                               'Original Left position of the Control.
  44.   OriginalTop As Long                                'Original Top position of the Control.
  45.   OriginalWidth As Long                              'Original Width of the Control.
  46.   OriginalHeight As Long                             'Original Height of the Control.
  47. End Type
  48.  
  49. ' ======================================================================================== '
  50. ' Enumeration Declarations.                                                                '
  51. ' ======================================================================================== '
  52.  
  53. ' ======================================================================================== '
  54. ' Event Declarations.                                                                      '
  55. ' ======================================================================================== '
  56.  
  57. ' ======================================================================================== '
  58. ' Public Variable Declarations.                                                            '
  59. ' ======================================================================================== '
  60.  
  61. ' ======================================================================================== '
  62. ' Private Variable Declarations.                                                           '
  63. ' ======================================================================================== '
  64. Private m_SourceForm As Form                         'The form to be resized.
  65. Private m_FormWidth As Long                          'Original form width.
  66. Private m_FormHeight As Long                         'Original form height.
  67. Private m_Controls() As ControlPosition              'Array for storing control information.
  68. Private m_IsFirstResize As Boolean                   'Flag indicating first resize.
  69.  
  70. ' ======================================================================================== '
  71. ' Routine     : Class_Initialize                                      Created : 01/05/1999 '
  72. ' Scope       : Private                                               Author  : A D Moss   '
  73. ' Description : Constructor used when an instance of this class is created.                '
  74. ' ======================================================================================== '
  75. Private Sub Class_Initialize()
  76.   Set m_SourceForm = Nothing
  77.  
  78.   m_IsFirstResize = True
  79. End Sub
  80.  
  81. ' ======================================================================================== '
  82. ' Routine     : Class_Terminate                                       Created : 01/05/1999 '
  83. ' Scope       : Private                                               Author  : A D Moss   '
  84. ' Description : Destructor used when an instance of this class is destroyed.               '
  85. ' ======================================================================================== '
  86. Private Sub Class_Terminate()
  87.   Erase m_Controls
  88.  
  89.   Set m_SourceForm = Nothing
  90. End Sub
  91.  
  92. ' ======================================================================================== '
  93. ' Routine     : SourceForm (Get)                                      Created : 01/05/1999 '
  94. ' Scope       : Public                                                Author  : A D Moss   '
  95. ' Description : Return the Form currently being used by an instance of this class.         '
  96. ' ======================================================================================== '
  97. Public Property Get SourceForm() As Form
  98.   Set SourceForm = m_SourceForm
  99. End Property
  100.  
  101. ' ======================================================================================== '
  102. ' Routine     : SourceForm (Let)                                      Created : 01/05/1999 '
  103. ' Scope       : Public                                                Author  : A D Moss   '
  104. ' Description : Set the Form to be used by an instance of this class.                      '
  105. ' ======================================================================================== '
  106. Public Property Let SourceForm(New_SourceForm As Form)
  107.   Set m_SourceForm = New_SourceForm
  108. End Property
  109.  
  110. ' ======================================================================================== '
  111. ' Routine     : SizeFormToScreen                                      Created : 01/05/1999 '
  112. ' Scope       : Public                                                Author  : A D Moss   '
  113. ' Description : Size the Form to n Percent of the Screen.                                  '
  114. ' ======================================================================================== '
  115. Public Sub SizeFormToScreen(Optional Percent As Byte = 100)
  116.   'Variable Declarations.
  117.   Dim FormHeight As Long, FormWidth As Long
  118.  
  119.   'Check if the form has not been resized before.
  120.   If m_IsFirstResize Then
  121.     Call SaveInitialStates
  122.   End If
  123.  
  124.   'Calculate the new height and width the form needs to be resized to, based on the current
  125.   'screen resolution.
  126.   FormHeight = Int(Screen.Height * (Percent / 100))
  127.   FormWidth = Int(Screen.Width * (Percent / 100))
  128.  
  129.   'Use the Form that is to be resized.
  130.   With m_SourceForm
  131.     'Change the demensions and position of the form.
  132.     .Top = (Screen.Height - FormHeight) / 2
  133.     .Left = (Screen.Width - FormWidth) / 2
  134.     .Height = FormHeight
  135.     .Width = FormWidth
  136.   End With
  137.  
  138.   'Resize all of the controls on the form.
  139.   Call ResizeControls
  140. End Sub
  141.  
  142. ' ======================================================================================== '
  143. ' Routine     : SaveInitialStates                                     Created : 01/05/1999 '
  144. ' Scope       : Private                                               Author  : A D Moss   '
  145. ' Description : Store the dimensions and positions of all controls on the Form.            '
  146. ' ======================================================================================== '
  147. Private Sub SaveInitialStates()
  148.   'Variable Declarations.
  149.   Dim i As Integer
  150.  
  151.   'Check if the form has any controls on it.
  152.   If m_SourceForm.Controls.Count = 0 Then Exit Sub
  153.  
  154.   'Use the form that is being resized.
  155.   With m_SourceForm
  156.     'Check if the form being resized is an MDI form.
  157.     If (TypeOf m_SourceForm Is MDIForm) Then
  158.       'Set the FormWidth and FormHeight variables to the Form's Width and Height.
  159.       m_FormWidth = .Width
  160.       m_FormHeight = .Height
  161.     Else
  162.       'Set the FormWidth and FormHeight variables to the Form's Scale Width and Height.
  163.       m_FormWidth = .ScaleWidth
  164.       m_FormHeight = .ScaleHeight
  165.     End If
  166.   End With
  167.  
  168.   'Frees the memory used by the dynamic array storing all control information.
  169.   Erase m_Controls
  170.  
  171.   'Redimension the array storing all control information to the number of controls on the
  172.   'form that is being resized.
  173.   ReDim m_Controls(0 To (m_SourceForm.Controls.Count - 1))
  174.  
  175.   'Iterate through each of the Controls on the form to be resized.
  176.   For i = 0 To (m_SourceForm.Controls.Count - 1)
  177.     'Use the control that is currently being referenced.
  178.     With m_SourceForm.Controls(i)
  179.       'Add the controls reference to the array containing all control information.
  180.       Set m_Controls(i).ControlInstance = m_SourceForm.Controls(i)
  181.  
  182.       'Check if the control currently being referenced is a line control.
  183.       If (TypeOf m_SourceForm.Controls(i) Is Line) Then
  184.         'Add a reference to the control and it's position into the Controls array.
  185.         m_Controls(i).OriginalLeft = .X1
  186.         m_Controls(i).OriginalTop = .Y1
  187.         m_Controls(i).OriginalWidth = .X2
  188.         m_Controls(i).OriginalHeight = .Y2
  189.       Else
  190.         On Error Resume Next
  191.           'Add a reference to the control and it's position into the Controls array.
  192.           m_Controls(i).OriginalLeft = .Left
  193.           m_Controls(i).OriginalTop = .Top
  194.           m_Controls(i).OriginalWidth = .Width
  195.           m_Controls(i).OriginalHeight = .Height
  196.         On Error GoTo 0
  197.       End If
  198.     End With
  199.   Next
  200.  
  201.   'Notify the class that the form has been resized.
  202.   m_IsFirstResize = False
  203. End Sub
  204.  
  205. ' ======================================================================================== '
  206. ' Routine     : ReInitialise                                          Created : 01/05/1999 '
  207. ' Scope       : Public                                                Author  : A D Moss   '
  208. ' Description : Store the dimensions and positions of all controls on a Form.              '
  209. ' ======================================================================================== '
  210. Public Sub ReInitialise()
  211.   Call SaveInitialStates
  212. End Sub
  213.  
  214. ' ======================================================================================== '
  215. ' Routine     : ResizeControls                                        Created : 01/05/1999 '
  216. ' Scope       : Public                                                Author  : A D Moss   '
  217. ' Description : Resize all of the Controls on a Form.                                      '
  218. ' ======================================================================================== '
  219. Public Sub ResizeControls()
  220.   'Variable Declarations.
  221.   Dim HeightChange As Double, WidthChange As Double
  222.   Dim ControlInstance As Control
  223.   Dim ResizeRepositionFlags As String
  224.   Dim i As Integer
  225.  
  226.   'Check if the form has any controls on it.
  227.   If m_SourceForm.Controls.Count = 0 Then Exit Sub
  228.  
  229.   'Check if the form is being minimized.
  230.   If m_SourceForm.WindowState = vbMinimized Then Exit Sub
  231.  
  232.   'Check if it's the first time the form is to be resized.
  233.   If m_IsFirstResize Then
  234.     Call SaveInitialStates
  235.     Exit Sub
  236.   End If
  237.  
  238.   'Prevent the Window from redrawing until the user releases the mouse.
  239.   Call LockWindowUpdate(m_SourceForm.hWnd)
  240.  
  241.   'Calculate the change in the form's size.
  242.   If (TypeOf m_SourceForm Is MDIForm) Then
  243.     HeightChange = m_SourceForm.Height / m_FormHeight
  244.     WidthChange = m_SourceForm.Width / m_FormWidth
  245.   Else
  246.     HeightChange = m_SourceForm.ScaleHeight / m_FormHeight
  247.     WidthChange = m_SourceForm.ScaleWidth / m_FormWidth
  248.   End If
  249.  
  250.   'Iterate through the array containing the form's controls.
  251.   For i = 0 To UBound(m_Controls)
  252.     Set ControlInstance = m_Controls(i).ControlInstance
  253.  
  254.     'Use the Control that is currently being referenced.
  255.     With ControlInstance
  256.       On Error Resume Next
  257.         'Check if the Tag Property contains valid resize or reposition instructions.
  258.         If (Left(UCase(.Tag), 1) = ResizeRepositionCommand) Then
  259.           'Retrieve any specific resize and reposition instructions.
  260.           ResizeRepositionFlags = UCase(.Tag)
  261.         Else
  262.           'Default all resize and resposition instructions.
  263.           ResizeRepositionFlags = ResizeRepositionCommand & ResizeRepositionLeft & ResizeRepositionTop & ResizeRepositionWidth & ResizeRepositionHeight
  264.         End If
  265.  
  266.         'Resize and Reposition the control.
  267.         If (TypeOf ControlInstance Is Line) Then
  268.           'Line control.
  269.            If (InStr(ResizeRepositionFlags, ResizeRepositionLeft) <> 0) Then .X1 = (m_Controls(i).OriginalLeft * WidthChange)
  270.            If (InStr(ResizeRepositionFlags, ResizeRepositionTop) <> 0) Then .Y1 = (m_Controls(i).OriginalTop * HeightChange)
  271.            If (InStr(ResizeRepositionFlags, ResizeRepositionWidth) <> 0) Then .X2 = (m_Controls(i).OriginalWidth * WidthChange)
  272.            If (InStr(ResizeRepositionFlags, ResizeRepositionHeight) <> 0) Then .Y2 = (m_Controls(i).OriginalHeight * HeightChange)
  273.         ElseIf (UCase(TypeName(ControlInstance.Container)) = SSTAB_TYPE_NAME) Then
  274.           'Control is placed on a SSTab Dialog Control.
  275.           If (.Left < 0) Then
  276.             If (m_Controls(i).OriginalLeft > 0) Then
  277.               m_Controls(i).OriginalLeft = m_Controls(i).OriginalLeft - SSTAB_DIALOG_OFFSET
  278.             End If
  279.  
  280.             If (InStr(ResizeRepositionFlags, ResizeRepositionLeft) <> 0) Then .Left = ((m_Controls(i).OriginalLeft + SSTAB_DIALOG_OFFSET) * WidthChange) - SSTAB_DIALOG_OFFSET
  281.           Else
  282.             If (m_Controls(i).OriginalLeft < 0) Then
  283.               m_Controls(i).OriginalLeft = m_Controls(i).OriginalLeft + SSTAB_DIALOG_OFFSET
  284.             End If
  285.  
  286.             If (InStr(ResizeRepositionFlags, ResizeRepositionLeft) <> 0) Then .Left = m_Controls(i).OriginalLeft * WidthChange
  287.           End If
  288.  
  289.           If (InStr(ResizeRepositionFlags, ResizeRepositionTop) <> 0) Then .Top = (m_Controls(i).OriginalTop * HeightChange)
  290.           If (InStr(ResizeRepositionFlags, ResizeRepositionWidth) <> 0) Then .Width = (m_Controls(i).OriginalWidth * WidthChange)
  291.           If (InStr(ResizeRepositionFlags, ResizeRepositionHeight) <> 0) Then .Height = (m_Controls(i).OriginalHeight * HeightChange)
  292.         Else
  293.           'All other controls.
  294.           If (InStr(ResizeRepositionFlags, ResizeRepositionLeft) <> 0) Then .Left = (m_Controls(i).OriginalLeft * WidthChange)
  295.           If (InStr(ResizeRepositionFlags, ResizeRepositionTop) <> 0) Then .Top = (m_Controls(i).OriginalTop * HeightChange)
  296.           If (InStr(ResizeRepositionFlags, ResizeRepositionWidth) <> 0) Then .Width = (m_Controls(i).OriginalWidth * WidthChange)
  297.           If (InStr(ResizeRepositionFlags, ResizeRepositionHeight) <> 0) Then .Height = (m_Controls(i).OriginalHeight * HeightChange)
  298.         End If
  299.       On Error GoTo 0
  300.     End With
  301.   Next i
  302.  
  303.   'Release the memory being used by the ControlInstance variable.
  304.   Set ControlInstance = Nothing
  305.  
  306.   'Allow Windows to refresh the form's display.
  307.   Call LockWindowUpdate(0)
  308. End Sub
  309.